package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.SubwayLineNewBean;

public class SubwaySelectLineAdapter extends RecyclerView.Adapter<SubwaySelectLineAdapter.ItemHolder> {

	private Context context;
	private List<SubwayLineNewBean> datas = new ArrayList<>();
	private int position = 0;
	ISubwaySelectLineClickListener iSubwaySelectLineClickListener;

	public SubwaySelectLineAdapter(Context context, List<String> list) {
		this.context = context;
		for (String st:list) {
			SubwayLineNewBean busStationNewBean = new SubwayLineNewBean();
			busStationNewBean.setTitle(st);
			this.datas.add(busStationNewBean);
		}
		datas.get(0).setSelect(true);
	}

	@Override
	public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(context).inflate(R.layout.item_classify_mainlist, parent, false);
		return new ItemHolder(view);
	}

	@Override
	public void onBindViewHolder(ItemHolder holder, int position) {
		SubwayLineNewBean bean = datas.get(position);
		String stantionname= bean.getTitle();
		if("一号线".equals(stantionname)){
			stantionname ="一号线";
			holder.img.setText("1");
			holder.img.setBackground(context.getResources().getDrawable(R.drawable.circle_subway_one));
		}
		if("二号线".equals(stantionname)){
			stantionname ="二号线";
			holder.img.setText("2");
			holder.img.setBackground(context.getResources().getDrawable(R.drawable.circle_subway_two));
		}
		if("三号线".equals(stantionname)){
			stantionname ="三号线";
			holder.img.setText("3");
			holder.img.setBackground(context.getResources().getDrawable(R.drawable.circle_subway_three));
		}
		if("四号线".equals(stantionname)){
			stantionname ="四号线";
			holder.img.setText("4");
			holder.img.setBackground(context.getResources().getDrawable(R.drawable.circle_subway_four));
		}
		if("四号线支线".equals(stantionname)){
			stantionname ="四号线支线";
			holder.img.setText("4");
			holder.img.setBackground(context.getResources().getDrawable(R.drawable.circle_subway_four));
		}
		holder.txt.setText(stantionname);

		if(bean.isSelect()){
			holder.layout.setBackgroundColor(0xFFFFFFFF);
		}else{
			holder.layout.setBackgroundColor(0xFFEBEBEB);
		}

		holder.layout.setOnClickListener(v -> {
			if(iSubwaySelectLineClickListener!=null){
				for(SubwayLineNewBean subwayLineNewBean: datas){
					subwayLineNewBean.setSelect(false);
				}
				bean.setSelect(true);
				notifyDataSetChanged();
				iSubwaySelectLineClickListener.onClick(position);
			}
		});

	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemCount() {
		return datas.size();
	}


	public void setSelectItem(int position) {
		this.position = position;
	}

	public int getSelectItem() {
		return position;
	}

	public static class ItemHolder extends RecyclerView.ViewHolder {
		LinearLayout layout;
		TextView img;
		TextView txt;

		public ItemHolder(View view) {
			super(view);
			txt = (TextView) view.findViewById(R.id.mainitem_txt);
			img = (TextView) view.findViewById(R.id.mainitem_img);
			layout = (LinearLayout) view.findViewById(R.id.mainitem_layout);
		}
	}

	public interface ISubwaySelectLineClickListener{
		void onClick(int position);
	}

	public void setiOnClickListener(ISubwaySelectLineClickListener iOnClickListener) {
		this.iSubwaySelectLineClickListener = iOnClickListener;
	}
}
